package com.ricchieri.manage.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.springframework.security.core.context.SecurityContextHolder;

import com.ricchieri.common.CustomAuthenticationToken;
import com.ricchieri.model.User;

@ManagedBean(name = "loggedUserMB")
@SessionScoped
public class LoggedUserManageBean extends GenericManageBean {

	private static final long serialVersionUID = -4192116597583059647L;


	public String getUsername() {
		if(SecurityContextHolder.getContext().getAuthentication() instanceof CustomAuthenticationToken) {
			CustomAuthenticationToken user = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication());
			return user.getUser().getFullName();
		} else {
			return "";
		}
	}
	
	public User getUser() {
		if(SecurityContextHolder.getContext().getAuthentication() instanceof CustomAuthenticationToken) {
			CustomAuthenticationToken user = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication());
			return user.getUser();
		} else {
			return null;
		}
	}
	
	
	public Boolean isAuthenticate() {
		if(SecurityContextHolder.getContext().getAuthentication() instanceof CustomAuthenticationToken) {
			CustomAuthenticationToken user = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication());
			if(user == null) {
				return Boolean.FALSE;
			} else {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}
}
